//
//  AppDelegate.m
//  YFZShoppingMall
//
//  Created by Coder on 16/7/5.
//  Copyright © 2016年 BJY12. All rights reserved.
//

#import "AppDelegate.h"
#import "HomeViewController.h"
#import "MainViewController.h"
#import "ViewController.h"
#import "AppConfig.h"
#import "LZXViewController.h"
#import "IndexMainViewController.h"
#import "WXApi.h"

// 友盟账号  59b8ac66f29d98590b000011

// ceshi
@interface AppDelegate ()<UIWebViewDelegate>
{
    MainViewController * indexMainController;
    
    
    
    
    ViewController     * VC;
    
    NSUserDefaults     * userDefaults;
    UIWebView          * webView;
    
    BOOL               * isFirstInto;
}

@end

@implementation AppDelegate

@synthesize tabBarController;

+ (AppDelegate *)getAPPDelegate
{
    return  (AppDelegate *)[[UIApplication sharedApplication] delegate];
}

- (UITabBarController *)getTarBar
{
    return  self.tabBarController;
}


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Override point for customization after application launch.
    
    
    NSLog(@"------------------------");
    
    self.window = [[UIWindow alloc]initWithFrame:[[UIScreen mainScreen] bounds]];
    
    userDefaults = [NSUserDefaults standardUserDefaults];
    
    
    //设置UINavigationBar的背景颜色和字体颜色
    //    [self customizeAppearance];
    
    //初始化Tarbar
    //    [self initTarbar];
    
    
    if(![[NSUserDefaults standardUserDefaults] boolForKey:@"firstStart"]){
        [[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"firstStart"];
        NSLog(@"第一次启动");
        
        // 初始化引导页控制器
        LZXViewController *view = [[LZXViewController alloc]init];
        // 设置引导页图片
        view.dataArray = [NSArray arrayWithObjects:@"guidePageImg1.png",@"guidePageImg2.png",@"guidePageImg3.png", nil];
        // 设置跳转界面
        view.controller = [[ViewController alloc] init];
        
        self.window.rootViewController = view;
        [self.window makeKeyAndVisible];
        
    }else{
        NSLog(@"不是第一次启动");
        VC = [[ViewController alloc] init];
        
        //不用导航栏
        UINavigationController*nav = [[UINavigationController alloc]initWithRootViewController:VC];
        
        self.window.rootViewController = nav;
        self.window.backgroundColor = [UIColor whiteColor];
        [self.window makeKeyAndVisible];
        
        
        
        
    }
    
    
    
    //向微信注册wxd930ea5d5a258f4f
    [WXApi registerApp:@"wx3a625486d6bc113e" withDescription:@"众乐城iOS客户端"];
    
//    [[UIApplication sharedApplication]setStatusBarHidden:YES withAnimation:UIStatusBarAnimationFade];
    
    
    return YES;
}

#pragma merk - 创建和设置TarBar
-(void)initTarbar
{
    //分类
    indexMainController = [[MainViewController alloc]initWithRootViewController:[[IndexMainViewController alloc]init]];
    
    //将功能视图存入数组
    NSArray *viewControllers=@[indexMainController];
    //图标数组
    NSArray *imageList=@[@"shouye"];
    //文字数组
    NSArray *titleList=@[@"首页"];
    
    //遍历封装成tabbarItem
    for (int i=0; i<viewControllers.count; i++) {
        UITabBarItem *tabItem=[[UITabBarItem alloc]initWithTitle:titleList[i] image:[UIImage imageNamed:imageList[i]] tag:i];
        MainViewController *viewController=viewControllers[i];
        viewController.tabBarItem=tabItem;
    }
    
    //原生UITabBarController
    tabBarController=[[UITabBarController alloc]init];
    [tabBarController setViewControllers:viewControllers animated:YES];
    
    
    //设置tabbar样式
    //设定Tabbar的点击后的颜色
    [[UITabBar appearance] setTintColor:[UIColor redColor]];
    
    self.window.rootViewController = tabBarController;
    
    
    //向微信注册wxd930ea5d5a258f4f
    [WXApi registerApp:@"wx3a625486d6bc113e" withDescription:@"众乐城iOS客户端"];
    
}

//设置UINavigationBar的背景颜色和字体颜色
- (void)customizeAppearance{
    
    if(SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"7.0"))
    {
        [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent];
        [[UINavigationBar appearance] setBarTintColor:UIMainNavi];
    }
    else
    {
        [[UINavigationBar appearance] setShadowImage:nil];
        [[UINavigationBar appearance] setBackgroundImage:[UIImage imageNamed:@"nav_bg"] forBarMetrics:UIBarMetricsDefault];
    }
    
    [[UINavigationBar appearance] setTitleTextAttributes:
     @{
       UITextAttributeTextColor: [UIColor blackColor]
       }];
}

- (void)applicationWillResignActive:(UIApplication *)application {
    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
    // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
}

- (void)applicationDidEnterBackground:(UIApplication *)application {
    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}

- (void)applicationWillEnterForeground:(UIApplication *)application {
    // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
}

//应用程序已经进入活动状态
- (void)applicationDidBecomeActive:(UIApplication *)application {
    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
    NSLog(@"应用程序已进入前台，处于活动状态");
    
    //发送一个通知刷新页面，防止更改了网络设置不显示
    [[NSNotificationCenter defaultCenter] postNotificationName:NFC_didBecomeActive object:nil];
    
}

- (void)applicationWillTerminate:(UIApplication *)application {
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}


- (void)showAlertForSelf:(NSString *)message :(int)tag{
    UIAlertView *alert = [[UIAlertView alloc]
                          initWithTitle:@"提示"
                          message:message
                          delegate:self
                          cancelButtonTitle:@"确定"
                          otherButtonTitles:nil
                          ];
    alert.tag = tag;
    [alert show];
}

- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation
{
    //获取支付宝或微信的支付结果
//    [[PayUtil sharedUtil] handleURL:url paymentCallback:^(PayResultType result, NSInteger payWay) {
//        if(result == LinkFinancepaySuccess)
//        {
//            [[NSNotificationCenter defaultCenter] postNotificationName:@"paysuccess" object:nil];
//            NSLog(@"paysuccess");
//        }
//        else
//        {
//            [[NSNotificationCenter defaultCenter] postNotificationName:@"payfail" object:nil];
//            NSLog(@"payfail");
//        }
//    }];

    return YES;
}


- (void)exitApplication {
    
    [UIView beginAnimations:@"exitApplication" context:nil];
    
    [UIView setAnimationDuration:0.5];
    
    [UIView setAnimationDelegate:self];
    
    // [UIView setAnimationTransition:UIViewAnimationCurveEaseOut forView:self.view.window cache:NO];
    
    [UIView setAnimationTransition:UIViewAnimationCurveEaseOut forView:self.window cache:NO];
    
    [UIView setAnimationDidStopSelector:@selector(animationFinished:finished:context:)];
    
    //self.view.window.bounds = CGRectMake(0, 0, 0, 0);
    
    self.window.bounds = CGRectMake(0, 0, 0, 0);
    
    [UIView commitAnimations];
    
}




@end
